package com.jsnkj.basic.fragment

import android.os.Bundle
import android.view.View
import com.jsnkj.basic.R
import com.jsnkj.basic.activity.WithdrawActivity
import com.jsnkj.basic.advertisement.AdvertisementManager
import com.jsnkj.basic.databinding.FragmentVideoBinding
import com.jsnkj.basic.mvvm.BaseMVVMFragment
import com.bumptech.glide.Glide
import com.jeremyliao.liveeventbus.LiveEventBus


class VideoFragment : BaseMVVMFragment<FragmentVideoBinding, VideoViewModel>(),
    View.OnClickListener {

    companion object {
        fun newInstance() = VideoFragment()
    }

    override val layoutId: Int
        get() = R.layout.fragment_video



    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this
        AdvertisementManager.loadHjVideoAds(parentFragmentManager,R.id.fragment_container);


        mVM.postLoginResult.observe(this) { userEntity ->
            com.jsnkj.basic.util.LogUtils.i("视频页得到的数据：$userEntity")
            userEntity.let { user ->
                if (user.avatar.isNullOrEmpty()){
                    Glide.with(requireActivity()).load(R.drawable.icon_user).into(mVDB.imgAvatar)
                }else{
                    Glide.with(requireActivity()).load(user.avatar).into(mVDB.imgAvatar)
                }

                mVDB.tvTopName.text = user.showName()
                mVDB.tvId.setText("会员ID：${user.uuid}")

                user.withdrawable_coins.let {
                    mVDB.txtTopMoney.text = it.toString()
                }
            }
        }



        LiveEventBus.get<String>("readFragmentRefresh").observe(this) {
            com.jsnkj.basic.util.LogUtils.e("刷新用户信息")
            mVM.postAuthUserInfo()
        }

    }


    private var isInitFragment = false

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        AdvertisementManager.loadHjVideoAds(parentFragmentManager,R.id.fragment_container);
        if (!isInitFragment) {
            return
        }
        com.jsnkj.basic.util.LogUtils.e("VideoFragment setUserVisibleHint  是否可见： $isVisibleToUser")

        if (isVisibleToUser) {
            mVM.postAuthUserInfo()
        }
    }


    override fun onResume() {
        super.onResume()
        isInitFragment = true

    }

    override fun onDestroy() {
        super.onDestroy()
        //TbVideoManager.cleanVideo()
    }


    override fun onClick(view: View) {
        when (view.id) {
            R.id.img_fd -> {
                com.jsnkj.basic.util.LogUtils.e("点击福袋")
                LiveEventBus.get<String>("openfd").post("1");

            }
            R.id.txt_withdrawal -> {
                startActivity(requireActivity(), WithdrawActivity::class.java)
            }
        }
    }

}